package com.netrust.quarz.task.schedled;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;

import com.netrust.quarz.task.BulidAlarmHistoryTask;
import com.netrust.quarz.task.BulidAlarmRealTask;
import com.netrust.quarz.task.BulidStateTask;

/**
 * 调度器bean
 * @author visitor
 *
 */
@Configuration
public class SchedledConfiguration {
	
	@Value("${task.trigger.bulidRealAlarm}")
	private String bulidRealAlarmTrigger;
	@Value("${task.trigger.bulidHistoryAlarm}")
	private String bulidHistoryAlarmTrigger;
	@Value("${task.trigger.bulidState}")
	private String bulidStateTrigger;
	
	//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓待处理告警↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
	@Bean(name = "bulidRealAlarmTaskFactoryBean")  
    public MethodInvokingJobDetailFactoryBean bulidRealAlarmTaskFactoryBean(BulidAlarmRealTask bulidAlarmRealTask){  
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();  
        //这儿设置对应的Job对象  
        bean.setTargetObject (bulidAlarmRealTask);  
        //这儿设置对应的方法名  与执行具体任务调度类中的方法名对应  
        bean.setTargetMethod ("init");  
        bean.setConcurrent (false);  
        return bean;  
    }  
  
    @Bean(name = "bulidRealAlarmCronTriggerBean")  
    public CronTriggerFactoryBean bulidRealAlarmCronTriggerBean(@Qualifier("bulidRealAlarmTaskFactoryBean") MethodInvokingJobDetailFactoryBean bulidRealAlarmTaskFactoryBean){  
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean ();  
        trigger.setJobDetail (bulidRealAlarmTaskFactoryBean.getObject ());  
        try {  
            trigger.setCronExpression (bulidRealAlarmTrigger);
        } catch (Exception e) {  
            e.printStackTrace ();  
        }  
        return trigger;  
    }

    //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓已处理告警↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    @Bean(name = "bulidHistoryAlarmTaskFactoryBean")  
    public MethodInvokingJobDetailFactoryBean bulidHistoryAlarmTaskFactoryBean(BulidAlarmHistoryTask bulidAlarmHistoryTask){  
    	MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();  
    	//这儿设置对应的Job对象  
    	bean.setTargetObject (bulidAlarmHistoryTask);  
    	//这儿设置对应的方法名  与执行具体任务调度类中的方法名对应  
    	bean.setTargetMethod ("init");  
    	bean.setConcurrent (false);  
    	return bean;  
    }  
    
    @Bean(name = "bulidHistoryAlarmCronTriggerBean")  
    public CronTriggerFactoryBean bulidHistoryAlarmCronTriggerBean(@Qualifier("bulidHistoryAlarmTaskFactoryBean") MethodInvokingJobDetailFactoryBean bulidHistoryAlarmTaskFactoryBean){  
    	CronTriggerFactoryBean trigger = new CronTriggerFactoryBean ();  
    	trigger.setJobDetail (bulidHistoryAlarmTaskFactoryBean.getObject ());  
    	try {  
    		trigger.setCronExpression (bulidHistoryAlarmTrigger);
    	} catch (Exception e) {  
    		e.printStackTrace ();  
    	}  
    	return trigger;  
    }
    
    //↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓运行状态与利用率↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
	@Bean(name = "bulidStateTaskFactoryBean")  
    public MethodInvokingJobDetailFactoryBean bulidStateTaskFactoryBean(BulidStateTask bulidStateTask){  
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();  
        //这儿设置对应的Job对象  
        bean.setTargetObject (bulidStateTask);  
        //这儿设置对应的方法名  与执行具体任务调度类中的方法名对应  
        bean.setTargetMethod ("init");  
        bean.setConcurrent (false);  
        return bean;  
    }  
  
    @Bean(name = "bulidStateCronTriggerBean")  
    public CronTriggerFactoryBean bulidStateCronTriggerBean(@Qualifier("bulidStateTaskFactoryBean") MethodInvokingJobDetailFactoryBean bulidStateTaskFactoryBean){  
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean ();  
        trigger.setJobDetail (bulidStateTaskFactoryBean.getObject ());  
        try {  
            trigger.setCronExpression (bulidStateTrigger);//每5秒执行一次  
        } catch (Exception e) {  
            e.printStackTrace ();  
        }  
        return trigger;  
    } 
    
}
